package com.hua.yuan.ui;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import com.hua.yuan.base.Constents;
import com.hua.yuan.common.BaseActivity;
import com.hua.yuan.R;
import com.hua.yuan.ui.data.PlayDataRequest;
import com.hua.yuan.util.ToastUtil;
import com.zhy.http.okhttp.callback.StringCallback;

import org.json.JSONException;
import org.json.JSONObject;

import cm.hua.yuan.common.utils.PreferenceUtils;
import okhttp3.Call;

/**
 * Created by huangyang on 2018/5/11.
 * 我的钱包的界面
 */

public class MoneyBagActivity extends BaseActivity implements View.OnClickListener {

    private ImageView backImage;
    private TextView allMoney;//全部的钱的按钮
    private TextView moneyKou;//扣钱的按钮

    private TextView rightTv;

    private String buyMoney = "";//购买的钱数
    private String movieId = "";//电影的id


    private MoneyBagActivity context;

    private int type = 0;//0代币充值 1代币扣去

    private boolean kMflag = true;
    private boolean isTuji = false;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_money_bag);

        context = this;

        buyMoney = getIntent().getStringExtra("buyMoney");
        movieId = getIntent().getStringExtra("movieId");
        isTuji = getIntent().getBooleanExtra("isTuji", false);

        initView();
        initData();


    }


    @Override
    protected void onResume() {
        super.onResume();
        checkMoney();

    }

    private void initView() {

        backImage = findViewById(R.id.backImage);
        allMoney = findViewById(R.id.allMoney);
        moneyKou = findViewById(R.id.moneyKou);
        rightTv = findViewById(R.id.rightTv);


    }

    private void initData() {

        backImage.setOnClickListener(this);
        moneyKou.setOnClickListener(this);
        rightTv.setOnClickListener(this);

    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.backImage:
                finish();
                break;
            case R.id.moneyKou:
                //点击扣钱
                if (type == 0) {
                    //充值代币界面
                    startActivity(new Intent(this, CZActivity.class).putExtra("money", allMoney.getText().toString().trim()));
                } else {
                    deleteMFun();
                }

                break;

            case R.id.rightTv:

//                ToastUtil.showShortToast(this, "开发中，敬请期待!");

                startActivity(new Intent(this, MyBuyActivity.class));
                break;


        }
    }


    /**
     * 检查钱
     */
    private void checkMoney() {

        String token = PreferenceUtils.getString(Constents.TOKEN);

        PlayDataRequest.queryMoney(token, new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {

                if (context != null) {
                    ToastUtil.showShortToast("查询代币数据出错");
                }

                if (moneyKou != null) {
                    moneyKou.setText("代币充值");
                }
                type = 0;

            }

            @Override
            public void onResponse(String response, int id) {

                try {
                    JSONObject jsonObject = new JSONObject(response);

                    if (!jsonObject.isNull("errno")) {
                        String code = jsonObject.getString("errno");

                        if ("0".equals(code)) {

                            if (!jsonObject.isNull("money")) {
                                String money = jsonObject.getString("money");

                                if (allMoney != null) {
                                    allMoney.setText(money);
                                }

                                if (!"-1".equals(movieId) && !"-1".equals(buyMoney)) {

                                    try {
                                        float allM = Float.parseFloat(money);
                                        float buyM = Float.parseFloat(buyMoney);

                                        if (allM >= buyM) {
                                            if (moneyKou != null) {
                                                moneyKou.setText("扣除代币");
                                            }
                                            type = 1;

                                        } else {
                                            if (moneyKou != null) {
                                                moneyKou.setText("代币充值");
                                            }
                                            type = 0;
                                        }


                                    } catch (NumberFormatException e) {
                                        e.printStackTrace();
                                        if (moneyKou != null) {
                                            moneyKou.setText("代币充值");
                                        }

                                        type = 0;
                                    }

                                } else {
                                    if (moneyKou != null) {
                                        moneyKou.setText("代币充值");
                                    }
                                    type = 0;
                                }


                            } else {
                                if (moneyKou != null) {
                                    moneyKou.setText("代币充值");
                                }
                                type = 0;

                                if (context != null) {
                                    ToastUtil.showShortToast("查询代币数据出错");
                                }
                            }

//

                        } else {
                            if (context != null) {
                                ToastUtil.showShortToast("查询代币数据出错");
                            }

                            if (moneyKou != null) {
                                moneyKou.setText("代币充值");
                            }
                            type = 0;
                        }

                    } else {
                        if (context != null) {
                            ToastUtil.showShortToast("查询代币数据出错");
                        }

                        if (moneyKou != null) {
                            moneyKou.setText("代币充值");
                        }
                        type = 0;

                    }

                } catch (JSONException e) {
                    e.printStackTrace();
                    if (context != null) {
                        ToastUtil.showShortToast("查询代币数据出错");
                    }

                    if (moneyKou != null) {
                        moneyKou.setText("代币充值");
                    }
                    type = 0;
                }


            }
        });


    }


    /**
     * 扣代币
     */
    private void deleteMFun() {

        if (!kMflag) {
            return;
        }

        kMflag = false;

        String token = PreferenceUtils.getString(null, Constents.TOKEN);

        PlayDataRequest.deleteMoney(isTuji, token, movieId, new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {

                ToastUtil.showShortToast("扣除代币失败");
                kMflag = true;
            }

            @Override
            public void onResponse(String response, int id) {

                try {
                    JSONObject jsonObject = new JSONObject(response);

                    if (!jsonObject.isNull("errno")) {
                        String code = jsonObject.getString("errno");

                        if ("0".equals(code)) {
                            ToastUtil.showShortToast("扣除代币成功");
                            sendBroadcast(new Intent("delete_con_success").putExtra("id", id));
                            finish();
                        } else {
                            ToastUtil.showShortToast( "扣除代币失败");

                        }
                    } else {
                        ToastUtil.showShortToast( "扣除代币失败");

                    }

                } catch (JSONException e) {
                    e.printStackTrace();
                    ToastUtil.showShortToast("扣除代币失败");

                }

                kMflag = true;
            }

        });
    }


}
